package top.goldenyear.porpoise.framework.web.core.interceptor;

import cn.hutool.v7.core.collection.CollUtil;
import java.util.Arrays;
import java.util.List;

import cn.hutool.v7.db.PageResult;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;
import top.goldenyear.porpoise.framework.web.core.annotation.Deform;

/**
 * @author airhead
 */
public class DeformInterceptor implements Interceptor {
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Override
  public Object doIntercept(Invocation inv) throws Throwable {
    Object ret = inv.invoke();

    //    Deform deform = inv.getMethodAnnotation(Deform.class);
    //    // 不需要转换
    //    if (deform == null) {
    //      return ret;
    //    }
    //
    //    // TODO: 如何避免范型擦除等问题？
    //    //    ParameterizedType[] generics = TypeUtil.getGenerics(ret.getClass());
    //
    ////    if (ret instanceof Model) {
    ////      if (CollUtil.isNotEmpty(Arrays.asList(deform.keep()))) {
    ////        Model model = (Model) ret;
    ////        return ModelUtils.keep(model, deform.keep());
    ////      }
    ////
    ////      if (CollUtil.isNotEmpty(Arrays.asList(deform.remove()))) {
    ////        Model model = (Model) ret;
    ////        return ModelUtils.keep(model, deform.keep());
    ////      }
    ////    }
    //
    //    if (ret instanceof PageResult) {
    //      if (CollUtil.isNotEmpty(Arrays.asList(deform.keep()))) {
    //        PageResult pageResult = (PageResult) ret;
    //        return ModelUtils.keep(pageResult.getRecords(), deform.keep());
    //      }
    //
    //      if (CollUtil.isNotEmpty(Arrays.asList(deform.remove()))) {
    //        PageResult pageResult = (PageResult) ret;
    //        return ModelUtils.remove(pageResult.getRecords(), deform.keep());
    //      }
    //    }
    //
    //    if (ret instanceof List) {
    //      if (CollUtil.isNotEmpty(Arrays.asList(deform.keep()))) {
    //        List list = (List) ret;
    //        return ModelUtils.keep(list, deform.keep());
    //      }
    //
    //      if (CollUtil.isNotEmpty(Arrays.asList(deform.remove()))) {
    //        List list = (List) ret;
    //        return ModelUtils.remove(list, deform.keep());
    //      }
    //    }

    return ret;
  }
}
